package com.test;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.reactive.RedisReactiveCommands;
import reactor.core.publisher.Mono;

// https://www.cnblogs.com/throwable/p/11601538.html
public class PingDemo {
	private Logger log = LoggerFactory.getLogger(PingDemo.class);
	private static StatefulRedisConnection<String, String> CONNECTION;
	private static RedisClient CLIENT;
	private static RedisReactiveCommands<String, String> REACTIVE_COMMAND;

	@BeforeClass
	public static void beforeClass() {
	    RedisURI redisUri = RedisURI.builder()
	            //.withHost("127.0.0.1")
	            //.withPort(6379)
	    		.withSentinel("10.16.34.125", 6406)
	            .withSentinelMasterId("sentinel-10.16.34.15-6381")
	            .withTimeout(Duration.of(1000, ChronoUnit.SECONDS))
	            .build();
	    CLIENT = RedisClient.create(redisUri);
	    CONNECTION = CLIENT.connect();
	    REACTIVE_COMMAND = CONNECTION.reactive();
	}

	@Test
	public void testReactivePing() throws Exception {
	    Mono<String> ping = REACTIVE_COMMAND.ping();
	    // [lettuce-nioEventLoop-4-1] INFO  com.test.Demo - Ping result:PONG
	    ping.subscribe(v -> {
	    	log.info("Ping result:{}", v);
	    });
	    Thread.sleep(1000);
	}

}
